package com.xqboss.apps.service.marketing;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xqboss.apps.domain.marketing.MarketingRightsCard;
import com.xqboss.apps.domain.marketing.vo.MarketingRightsCardVO;
import com.xqboss.apps.domain.marketing.vo.PageResponseVO;
import com.xqboss.apps.domain.nft.NftOld;
import com.xqboss.apps.mapper.marketing.MarketingRightsCardMapper;
import com.xqboss.apps.service.nft.NftOldService;
import com.xqboss.apps.util.mh.BeanCopierUtil;
import com.xqboss.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fu.yang
 * @since 2022-08-08
 */
@Service
public class MarketingRightsCardService extends ServiceImpl<MarketingRightsCardMapper, MarketingRightsCard> implements IService<MarketingRightsCard> {

    public static final String GRAND_CARD_KEY = "grand_card_key_";
    public static final String GRAND_CARD_LOCK = "grand_card_lock_";
    /**
     * 权益卡库存信号量key
     */
    public static final String CARD_STOCK_SEMAPHORE = "card_stock_semaphore_";
    @Resource
    private MarketingRightsCardMapper marketingRightsCardMapper;
    @Autowired
    private NftOldService nftOldService;


    /**
     * 选择列表分页方法
     *
     * @param title
     * @param id
     * @param count
     * @param page
     * @return
     */
    public PageResponseVO<MarketingRightsCardVO> selectPageList(String title, Integer id, Integer count, Integer page) {
        PageHelper.startPage(page, count);
        List<MarketingRightsCard> cardList = this.list(new LambdaQueryWrapper<MarketingRightsCard>()
                .like(StrUtil.isNotBlank(title), MarketingRightsCard::getTitle, title)
                .eq(id != null, MarketingRightsCard::getId, id)
                .eq(MarketingRightsCard::getStatus, MarketingRightsCard.StatusEnum.GRANTING.getValue()));
        List<MarketingRightsCardVO> cardVOS = BeanCopierUtil.copylist(cardList, MarketingRightsCardVO.class);
        cardVOS.stream().forEach(cardVO -> buildVoDetail(cardVO));
        PageInfo<MarketingRightsCard> pageInfo = new PageInfo<>(cardList);
        PageResponseVO pageResponseVO = new PageResponseVO(pageInfo.getTotal(), cardVOS, pageInfo.getPageNum(), pageInfo.getPageSize());
        return pageResponseVO;
    }

    /**
     * 构建详细信息 发放周期 已发放数量 权益卡内容
     *
     * @param
     * @param cardVO
     */
    private void buildVoDetail(MarketingRightsCardVO cardVO) {
        cardVO.setSendNum(cardVO.getStock() - cardVO.getCurrentStock());
        cardVO.setContent(cardVO.getTimesCount() + MarketingRightsCard.TypeEnum.getSeparator() +
                MarketingRightsCard.TypeEnum.getByValue(cardVO.getType()).getName());
        // 设置权益卡发放周期
        String period = "";
        switch (MarketingRightsCard.GrantMethodEnum.getByValue(cardVO.getGrantMethod())) {
            case SINGLE_USE:
                period = "一次到账";
                break;
            case BY_THE_DAY:
                period = String.format(MarketingRightsCard.GrantMethodEnum.getDayTemp(), cardVO.getEveryTime());
                break;
            case BY_THE_WEEK:
                period = String.format(MarketingRightsCard.GrantMethodEnum.getWeekTemp(), cardVO.getGrantDate(), cardVO.getEveryTime());
                break;
            case BY_THE_MONTH:
                period = String.format(MarketingRightsCard.GrantMethodEnum.getMonthTemp(), cardVO.getGrantDate(), cardVO.getEveryTime());
        }
        cardVO.setPeriod(period);
    }


    /**
     * 查询营销权益
     *
     * @param id 营销权益主键
     * @return 营销权益
     */
    public MarketingRightsCard selectMarketingRightsCardById(Long id) {
        return marketingRightsCardMapper.selectMarketingRightsCardById(id);
    }

    /**
     * 查询营销权益列表
     *
     * @param marketingRightsCard 营销权益
     * @return 营销权益
     */
    public List<MarketingRightsCardVO> selectMarketingRightsCardList(MarketingRightsCard marketingRightsCard) {
        List<MarketingRightsCard> rightsCardList = marketingRightsCardMapper.selectMarketingRightsCardList(marketingRightsCard);
        List<MarketingRightsCardVO> cardVOList = rightsCardList.stream().map(rightsCard -> {
            MarketingRightsCardVO cardVO = BeanCopierUtil.copy(rightsCard, MarketingRightsCardVO.class);

            int spuCount = nftOldService.list(new LambdaQueryWrapper<NftOld>()
                    .eq(NftOld::getGiftType, 1).eq(NftOld::getGiftId, rightsCard.getId())).size();
            cardVO.setSpuCount(spuCount);
            // 构建详细信息
            buildVoDetail(cardVO);
            return cardVO;
        }).collect(Collectors.toList());
        return cardVOList;
    }

    /**
     * 新增营销权益
     *
     * @param marketingRightsCard 营销权益
     * @return 结果
     */
    public int insertMarketingRightsCard(MarketingRightsCard marketingRightsCard) {
        marketingRightsCard.setCreateTime(DateUtils.getNowDate());
        return marketingRightsCardMapper.insertMarketingRightsCard(marketingRightsCard);
    }

    /**
     * 修改营销权益
     *
     * @param marketingRightsCard 营销权益
     * @return 结果
     */
    public int updateMarketingRightsCard(MarketingRightsCard marketingRightsCard) {
        marketingRightsCard.setUpdateTime(DateUtils.getNowDate());
        return marketingRightsCardMapper.updateMarketingRightsCard(marketingRightsCard);
    }

    /**
     * 批量删除营销权益
     *
     * @param ids 需要删除的营销权益主键
     * @return 结果
     */
    public int deleteMarketingRightsCardByIds(Long[] ids) {
        return marketingRightsCardMapper.deleteMarketingRightsCardByIds(ids);
    }

    /**
     * 删除营销权益信息
     *
     * @param id 营销权益主键
     * @return 结果
     */
    public int deleteMarketingRightsCardById(Long id) {
        return marketingRightsCardMapper.deleteMarketingRightsCardById(id);
    }
}
